<?php

class UserModel extends Model {

    private $auth = null;

    public function __construct() {
        parent::__construct();
        
        if (is_null($auth)) {
            $this->auth = M('users');
        }
    }

    public function register($user=array()) {
        if (!isset($user['username']) || $user['username'] == '' ) { return array('status' => false, 'errmsg' => 'username is required'); }
        if (!isset($user['email']) || $user['email'] == '') { return array('status' => false, 'errmsg' => 'email is required'); }
        if (!isset($user['phone']) || $user['phone'] == '') { return array('status' => false, 'errmsg' => 'phone number is required'); }
        if (!isset($user['password']) || $user['password'] == '') { return array('status' => false, 'errmsg' => 'password is required'); }
        if ($user['password'] != $user['passwordagain']) { return array('status' => false, 'errmsg' => 'password confirm is not matched'); }

        $username = $user['username'];
        $phone = $user['phone'];
        $email = $user['email'];
        $password = $user['password'];
        $salt = sha1(time());

        if ($this->findUserByName($username)) { return array('status' => false, 'errmsg' => 'username have been used'); }
        if ($this->findUserByPhone($phone)) { return array('status' => false, 'errmsg' => 'phone have been used'); }
        if ($this->findUserByEmail($email)) { return array('status' => false, 'errmsg' => 'email have been used'); }

        $password = pwdHash($username, $salt, $password);

        $afteruser = array(
            'username' => $username,
            'email' => $email,
            'phone' => $phone,
            'password' => $password,
            'salt' => $salt,
            'reg_date' => null,
            'reg_ip' => getClientIp(),
        );

        $result = $this->auth->data($afteruser)->add();

        if ($result) {
            return array('status' => true);
        } else {
            return array('status' => false, 'errmsg' => 'Internal error');
        }
    }

    public function login($user=array()) {
        
        $uid = isset($user['uid']) ? $user['uid'] : null;
        $username = isset($user['username']) ? $user['username'] : null;
        $password = isset($user['password']) ? $user['password'] : null;
        $phone = isset($user['phone']) ? $user['phone'] : null;

        $oneStepPass = false;
        $method = '';
        $errmsg = '';
        $status = false;

        if (!is_null($uid)) {
            if ($result = findUserById($uid)) {
                $oneStepPass = true;
                $method = 'uid';
            } else {
                $errmsg = is_null($result) ? 'uid is not valide' : 'uid is not exist';
            }
        }

        if (!is_null($username)) {
            if ($result = $this->findUserByName($username)) {
                $oneStepPass = true;
                $method = 'username';
            } else {
                $errmsg = is_null($result) ? 'username is not valide' : 'username is not exist';
            }
        }

        if (!is_null($phone)) {
            if ($result = findUserByPhone($phone)) {
                $oneStepPass = true;
                $method = 'phone';
            } else {
                $errmsg = is_null($result) ? 'phone is not valide' : 'phone is not exist';
            }
        }
        
        if (is_null($password)) { $status = false; return array('status' => $status, 'errmsg' => $errmsg); }
        if (!$oneStepPass) { $status = false; return array('status' => $status, 'errmsg' => $errmsg); }
        
        switch ($method) {
            case 'uid':
                $condition = array('uid' => $uid);
                $salt = $this->auth->field('salt')->where($condition)->find()['salt'];
                $fakeusername = $auth->where("uid = $uid")->field('username')->find();
                $fakepassword = pwdHash($fakeusername, $salt, $password);
                $condition = array("uid" => $uid, "password" => $fakepassword);
                break;

            case 'username':
                $condition = array('username' => $username);
                $salt = $this->auth->field('salt')->where($condition)->find()['salt'];
                $fakeusername = $username;
                $fakepassword = pwdHash($fakeusername, $salt, $password);
                $condition = array("username" => $username, "password" => $fakepassword);
                break;

            case 'phone':
                $condition = array('phone' => $phone);
                $salt = $this->auth->field('salt')->where($condition)->find()['salt'];
                $fakeusername = $auth->where("phone = $phone")->field('username')->find();
                $fakepassword = $pwdHash($fakeusername, $salt, $password);
                $condition = array("phone => $phone", "password" => $fakepassword);
        }

        $result = $this->auth->field('uid')->where($condition)->find();

        if ($result != null) {
            $status = true;
            $errmsg = $result;
            return array('status' => $status, 'errmsg' => $errmsg);
        } else {
            $status = false;
            $errmsg = 'password error';
            return array('status' => $status, 'errmsg' => $errmsg);
        }
    }

    public function revert() {

    }

    protected function findUserById($uid=null) {
        
        if (is_null($uid)) { return null; }
        $data = $this->auth->field('uid')->find($uid);

        if ($data != 0) {
            return true;
        } else {
            return false;
        }
    }

    public function getUserById($uid=null) {
        if (is_null($uid)) { return null; }
        $field = 'uid, username, email, phone, gender, birthdate, salt, reg_date, is_root, is_baned,is_actived, last_login_ip, last_login_date, token, expire_date';
        $condition = array('uid' => $uid);
        $data = $this->auth->field($field)->where($condition)->select();
        
        return $data[0];
    }

    protected function findUserByName($username=null) {
        
        if (is_null($username)) { return null; }
        $condition = array('username' => $username);
        $data = $this->auth->field('uid')->where($condition)->find();

        if ($data != 0) {
            return true;
        } else {
            return false;
        }
    }

    protected function findUserByEmail($email=null) {
        
        if (is_null($email)) { return null; }
        // TODO validate email
        $data = $this->auth->field('uid')->where("email = $email")->find();

        if ($data != 0) {
            return true;
        } else {
            return false;
        }
    }

    protected function findUserByPhone($phone=null) {
        
        if (is_null($phone)) { return null; }
        // TODO validate phone
        $data = $this->auth->field('uid')->where("phone = $phone")->find();

        if ($data != 0) {
            return true;
        } else {
            return false;
        }
    }

    protected function isSessionExpired($uid=null, $uname=null, $token) {
        
    }
}
